package com.fyzk.activity.lottery.jc.football.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fyzk.activity.lottery.jc.football.JCInfo;
import com.fyzk.activity.lottery.jc.football.newJz.data.NewScoreDetailActivity;
import com.tyjc.lottery.R;

import java.text.SimpleDateFormat;
import java.util.List;

public class JCZQ_BaseAdapter extends BaseAdapter {

    private String TAG = this.getClass().getSimpleName();
    boolean is_xiangqing;

    private List<JCInfo> infos;
    // private Handler handler;
    private LayoutInflater myInflater;
    protected Context context;

    private int lotteryId;

    public JCZQ_BaseAdapter(Context context, int lotteryId, List<JCInfo> infos) {
        this.infos = infos;
        this.context = context;
        this.lotteryId = lotteryId;
        myInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return infos.size();
    }

    @Override
    public Object getItem(int position) {
        return infos.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    int indext;

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        indext = position;
        final JCInfo infor = infos.get(position);
        final ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = myInflater.inflate(R.layout.zcbf_list, null);
            holder.ll_xinxi = (LinearLayout) convertView
                    .findViewById(R.id.zcbf_list_left);
            holder.ll_xiangqing = (LinearLayout) convertView
                    .findViewById(R.id.zcbf_xiangqing);
            holder.ll_right = (LinearLayout) convertView
                    .findViewById(R.id.jczq_list_right);
            holder.left_tv1 = (TextView) convertView
                    .findViewById(R.id.zcbf_list_teamname);
            holder.left_tv2 = (TextView) convertView
                    .findViewById(R.id.zcbf_list_teamnum);
            holder.left_tv3 = (TextView) convertView
                    .findViewById(R.id.zcbf_list_endtime);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }


        holder.ll_right.removeAllViews();
        holder.ll_right.addView(addRight(infor, myInflater, position));
        // 事件
        holder.left_tv1.setText(infor.getMatchName());
        holder.left_tv2.setText(infor.getMatchNum() + "");
        SimpleDateFormat format = new SimpleDateFormat("HH:mm");
        holder.left_tv3.setText(format.format(infor.getEndSaleTime()) + "[截]");


        if (infor.isShowHideView) {
            holder.ll_xiangqing.setVisibility(View.VISIBLE);
        } else {
            holder.ll_xiangqing.setVisibility(View.GONE);
        }
        if (infor.hideView != null) {
            ViewGroup parent1 = (ViewGroup) infor.hideView.getParent();
            if (parent1 != null) {
                parent1.removeAllViews();
            }
            holder.ll_xiangqing.removeAllViews();
            holder.ll_xiangqing.addView(infor.hideView);
        }

        // 点击显示隐藏信息
        holder.ll_xinxi.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
//                if (infor.hideView == null) {
//                    infor.hideView = new FootballMatchSummaryView(context, infor.getMatchNum());
//                    holder.ll_xiangqing.removeAllViews();
//                    holder.ll_xiangqing.addView(infor.hideView);
//                }
//                infor.isShowHideView = !infor.isShowHideView;
//                if (infor.isShowHideView) {
//                    holder.ll_xiangqing.setVisibility(View.VISIBLE);
//                } else {
//                    holder.ll_xiangqing.setVisibility(View.GONE);
//                }
                Intent intent = new Intent(context, NewScoreDetailActivity.class);
                intent.putExtra("isFootball", true);
                intent.putExtra("matchNum", infor.getMatchNum());
                context.startActivity(intent);
            }

        });

//        holder.ll_xiangqing.setOnClickListener(new OnClickListener() {
//
//            @Override
//            public void onClick(View v) {
//                // onClick1(JCZQ_BF_Activity.WHAT_FENXI, position);
//                Intent intent = new Intent(context, ScoreDetailActivity.class);
//                intent.putExtra("matchNum", infor.getMatchNum());
//                context.startActivity(intent);
//            }
//        });
        return convertView;
    }

    public void setInfos(List<JCInfo> infos, int type) {
        switch (type) {
            case 0:
//			 this.infos.clear();
//			 ArrayList<JCInfo> copyInfos = new ArrayList<JCInfo>(infos);
//			 for (JCInfo info : copyInfos) {
//			 if (info.getSupport().contains(lotteryId+"")) {
//			 this.infos.add(info);
//			 }
//			 }
                this.notifyDataSetChanged();
                break;
            case 1:

                break;
            case 2:

                break;
            default:
                break;
        }

    }

    protected View addRight(JCInfo infor, LayoutInflater myInflater,
                            final int position) {
        View view = myInflater.inflate(R.layout.jczq_list_right, null);
        TextView zhanKai;
        TextView right_tv1;
        TextView right_tv2;

        zhanKai = (TextView) view.findViewById(R.id.jczq_list_zhankai);
        right_tv1 = (TextView) view.findViewById(R.id.jczq_list_right_tv1);
        right_tv2 = (TextView) view.findViewById(R.id.jczq_list_right_tv2);

        right_tv1.setText(infor.getHomeTeam());
        right_tv2.setText(infor.getAwayTeam());
        zhanKai.setText(infor.getResult());
        // 打开比分投注对话框
//        zhanKai.setOnClickListener(new OnClickListener() {
//
//            @Override
//            public void onClick(View v) {
//                if (listener != null) {
//                    listener.onClick_ZhanKai(position);
//                }
//            }
//        });
        return view;
    }

    public interface OnJCZQAdapterClick {
        void onClick_xiangXi();

        void onClick_ZhanKai(int childposition);

        void onClick_ball(int position);
    }

    class ViewHolder {
        LinearLayout ll_xinxi;
        LinearLayout ll_xiangqing;
        LinearLayout ll_right;

        TextView left_tv1;
        TextView left_tv2;
        TextView left_tv3;

    }

}
